<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增实际工日单价')" />
    <th:block th:include="include :: datetimepicker-css" />
    <style type="text/css">
        table{
            border: 1px solid;
            width: 100%;
        }

        table td,th{
            border: 0.0625rem solid;
            text-align: center;
        }
        .number-text{
            width:100%;
            text-align:center;
            border:none;
        }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-price-add">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox">
                    <div class="ibox-title">
                        <h5>基础信息</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-sm-3 control-label is-required">单价日期：</label>
                            <div class="col-sm-8">
                                <div class="input-group date">
                                    <input required name="unitPriceDate" id="unitPriceDate" class="form-control" placeholder="yyyy-MM" type="text">
                                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">备注：</label>
                            <div class="col-sm-8">
                                <textarea name="remark" id="remark" class="form-control"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox">
                    <div class="ibox-title">
                        <h5>实际工日单价信息</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="form-group">
                            <div class="col-sm-12">
                                <div class="container-div" >
                                    <table id="priceTb">
                                        <tr>
                                            <th colspan="2">工程类别</th>
                                            <th>平均单价（元/工日）</th>
                                        </tr>
                                        <tr>
                                            <td rowspan="5">[[${categorys[0].name}]]</td>
                                            <td>[[${categorys[0].children[0].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[0].children[0].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[0].children[0].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[0].children[0].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[0].children[0].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[0].children[1].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[0].children[1].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[0].children[1].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[0].children[1].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[0].children[1].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[0].children[2].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[0].children[2].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[0].children[2].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[0].children[2].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[0].children[2].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[0].children[3].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[0].children[3].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[0].children[3].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[0].children[3].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[0].children[3].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[0].children[4].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[0].children[4].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[0].children[4].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[0].children[4].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[0].children[4].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td rowspan="6">[[${categorys[1].name}]]</td>
                                            <td>[[${categorys[1].children[0].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[1].children[0].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[1].children[0].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[1].children[0].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[1].children[0].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[1].children[1].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[1].children[1].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[1].children[1].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[1].children[1].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[1].children[1].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[1].children[2].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[1].children[2].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[1].children[2].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[1].children[2].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[1].children[2].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[1].children[3].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[1].children[3].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[1].children[3].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[1].children[3].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[1].children[3].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[1].children[4].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[1].children[4].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[1].children[4].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[1].children[4].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[1].children[4].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[1].children[5].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[1].children[5].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[1].children[5].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[1].children[5].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[1].children[5].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td rowspan="3">[[${categorys[2].name}]]</td>
                                            <td>[[${categorys[2].children[0].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[2].children[0].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[2].children[0].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[2].children[0].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[2].children[0].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[2].children[1].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[2].children[1].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[2].children[1].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[2].children[1].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[2].children[1].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[2].children[2].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[2].children[2].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[2].children[2].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[2].children[2].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[2].children[2].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td rowspan="3">[[${categorys[3].name}]]</td>
                                            <td>[[${categorys[3].children[0].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[3].children[0].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[3].children[0].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[3].children[0].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[3].children[0].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[3].children[1].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[3].children[1].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[3].children[1].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[3].children[1].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[3].children[1].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                        <tr>
                                            <td>[[${categorys[3].children[2].name}]]</td>
                                            <input type="hidden" name="categoryId" th:value="${categorys[3].children[2].id}"  />
                                            <input type="hidden" name="unitPriceId" th:value="${categorys[3].children[2].unitPriceId}"  />
                                            <input type="hidden" name="id" th:value="${categorys[3].children[2].detailId}"  />
                                            <td><input type="number" name="avgPrice" th:value="${categorys[3].children[2].avgPrice}" class="number-text" required/></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "category/detailreal"
    var datas = [[${@dict.getType('sys_normal_disable')}]];
    $("#form-price-add").validate({
        onkeyup: false,
        rules:{
            unitPriceDate:{
                remote: {
                    url: prefix + "/checkUnitPriceDateUnique?type=2",
                    type: "post",
                    dataType: "json",
                    data: {
                        "unitPriceDate": function() {
                            return $("input[name='unitPriceDate']").val();
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            avgPrice:{
                    isNumber : true
            }
        },
        messages: {
            "unitPriceDate": {
                remote: "当前月份实际工日单价已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form("form-price-add")) {
            var param =  {};
            param["detailList"] = getTableData();
            param["remark"] = $("#remark").val();
            param["unitPriceDate"] = $("#unitPriceDate").val();
            param["type"] = 2;
            $.operate.saveJson(prefix + "/add", JSON.stringify(param));
        }
    }

    $("input[name='unitPriceDate']").datetimepicker({
        format: "yyyy-mm",
        format: "yyyy-mm",
        startView:'year',
        maxView:'year',
        minView:'year',
        autoclose: true
    });
    $("input[name='unitPriceDate']").change(function(){
        $("input[name='unitPriceDate']").focus();
    });
    function getTableData(){
        var data = [];
        $('#priceTb tr').each(function(i){  // 遍历 tr
            var el = {};
            $(this).children('td').each(function(j){  // 遍历 tr 的各个 td
                if($(this).find("input[name='avgPrice']").length > 0){
                    el["avgPrice"] = $(this).find("input[name='avgPrice']").val();
                }
            });
            if($(this).find("input").length > 0){
                el["categoryId"] = $(this).find("input").val();
                data.push(el);
            }
        });
        return data;
    }
</script>
</body>
</html>